<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>

</head>
<body>
<div id="app">


    <!--搜索表单-->

    <el-form :inline="true" :model="searchMap" class="demo-form-inline" label-width="80px" size="mini">

        <el-form-item label="当前状态">
            <el-select v-model="searchMap.state" placeholder="当前状态">
                <el-option label="使用中" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
            </el-select>
        </el-form-item>

        <el-form-item label="部门名称">
            <el-input v-model="searchMap.deptName" placeholder="请输入部门名称"></el-input>
        </el-form-item>


        <el-form-item>
            <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>

        <el-button type="primary" style="float: right" size="small"   @click="dialogVisible = true;dept={}">新增</el-button>
    </el-form>



    <!--添加数据对话框表单-->
    <el-dialog
            title="编辑部门"
            :visible.sync="dialogVisible"
            width="30%"
    >

        <el-form ref="form" :model="dept" label-width="80px" size="mini">
            <el-form-item label="部门名称">
                <el-input v-model="dept.deptName"></el-input>
            </el-form-item>

            <el-form-item label="所属部门">
                <el-select v-model="dept.parentId" placeholder="所属部门">
                    <el-option v-for="dept in deptlist" :label="dept.deptName" :value="dept.deptId"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="状态">
                <el-switch v-model="dept.state"
                           active-value="1"
                           inactive-value="0"
                           active-text="使用中"
                           inactive-text="禁用"
                ></el-switch>
            </el-form-item>


            <el-form-item>
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="addDept">提交</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>


    <!--表格-->
    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange"
        >

            <el-table-column
                    prop="deptName"
                    label="部门名称"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="parentName"
                    label="所属部门"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="state"
                    align="center"
                    label="状态">
            </el-table-column>

            <el-table-column
                    align="center"
                    label="操作">

                <template slot-scope="scope">
                    <el-row>
                        <el-button type="primary" size="mini" @click="editDept(scope.row)">修改</el-button>
                        <el-button type="danger" size="mini" @click="delDept(scope.row)">删除</el-button>
                    </el-row>
                </template>

            </el-table-column>

        </el-table>
    </template>

    <!--分页工具条-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page.currentPage"
            :page-sizes="[5, 10, 20, 40]"
            :page-size="page.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total">
    </el-pagination>

</div>

<script src="../../js/vue.js"></script>
<script src="../../element-ui/lib/index.js"></script>
<link rel="stylesheet" href="../../element-ui/lib/theme-chalk/index.css">
<script src="../../js/axios-0.18.0.js"></script>

<script>
    new Vue({
        el: "#app",
        methods: {
            //删除用户
            delDept(row){
                this.$confirm('您确定删除该用户吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //点击确定 ，可以发送ajax请求进行删除
                    axios({
                        method:"delete",
                        url:"http://localhost:9000/dept/"+row.deptId
                    }).then((res)=>{
                        console.log(res.data);
                        if(res.data.flag){//删除成功
                            this.$message({
                                type: 'success',
                                message: res.data.message
                            });
                        }else {//删除失败
                            this.$message.error(res.data.message);;
                        }
                    }).catch((error)=>{

                    });

                }).catch(() => {
                    //点击取消
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });


            },
            //查询所有部门
            queryAll(){
                axios({
                    method:"get",
                    url:"http://localhost:9000/dept/listall"
                }).then((res)=>{
                    console.log(res.data);
                    this.deptlist= res.data;
                }).catch((error)=>{

                });
            },
            //条件查询部门
            queryDept(){
                let reqData = {
                    "currentPage":this.page.currentPage,
                    "pageSize":this.page.pageSize,
                    "deptName":this.searchMap.deptName,
                    "status":this.searchMap.status
                };
                axios({
                    method:"post",
                    url:"http://localhost:9000/dept/list",
                    data:reqData
                }).then((res)=>{
                    console.log(res.data);
                    this.tableData= res.data.rows;
                    this.page.total = res.data.total;
                }).catch((error)=>{

                });

            },
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            // 复选框选中后执行的方法
            handleSelectionChange(val) {
                this.multipleSelection = val;
                console.log(this.multipleSelection)
            },
            // 查询方法
            search() {
                console.log(this.searchMap);
                this.page.currentPage=1;
                this.queryDept();
            },
            // 添加数据
            addDept(){
                console.log(this.dept);
                this.dialogVisible = false;
                let url="";
				let methodType = "";
                if(this.dept.deptId){
                    url = "http://localhost:9000/dept/update";
					methodType="put";
                }else {
                    url = "http://localhost:9000/dept/add";
					methodType="post";
                }
                axios({
                    method:methodType,
                    url:url, // /user/1001
                    data: this.dept
                }).then((res)=>{
                    console.log(res.data);
                    if(res.data.flag){//删除成功
                        this.$message({
                            type: 'success',
                            message: res.data.message
                        });
                    }else {//删除失败
                        this.$message.error(res.data.message);;
                    }
                }).catch((error)=>{

                });
            },

            //分页
            handleSizeChange(val) {
                this.page.pageSize = val;
                this.queryDept();
            },
            handleCurrentChange(val) {
                this.page.currentPage = val;
                this.queryDept();
            },
            //根据id查询部门
            editDept(row){
                this.dialogVisible = true;
                axios({
                    method:"get",
                    url:"http://localhost:9000/dept/"+row.deptId
                }).then((res)=>{
                    console.log(res.data);
                    this.dept= res.data;
                }).catch((error)=>{

                });
            }

        },
        data() {
            return {
                page:{
                    pageSize:10,
                    total:100,
                    currentPage:1
                },
                // 添加数据对话框是否展示的标记
                dialogVisible: false,
                searchMap:{
                },
                // 部门模型数据
                dept: {
                    deptName: '',
                    parentId: '',
                    state:null
                },
                // 复选框选中数据集合
                multipleSelection: [],
                // 表格数据
                tableData: [],
                // 部门列表
                deptlist:[]
            }
        },
        mounted(){
            this.queryDept();
            this.queryAll();
        }
    })

</script>

</body>
</html>
